﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SaveTheQueen.Component
{
    public class ImageSprite : Sprite
    {
        private string resource;
        private Texture2D texture;

        public Texture2D Texture
        {
            get { return texture; }
            protected set { texture = value; CalcScaleFactor(Size); CalcCenterAndCenterOffset(sourceSize()); }
        }
        protected override Vector2 sourceSize()
        {
            if (texture != null)
                return new Vector2(texture.Width, texture.Height);
            else
                return Vector2.Zero;
        }


        public ImageSprite(string resource, int x, int y, int w, int h, GameObject owner, Game game)
            : base(x, y, w, h, owner, game)
        {
            this.resource = resource;
        }

        protected override void LoadContent()
        {
            texture = Game.Content.Load<Texture2D>(resource);

            base.LoadContent();
        }
        protected override void UnloadContent()
        {
            texture.Dispose();
            base.UnloadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteManager sm = ((SpriteManager)Game.Services.GetService(typeof(SpriteManager)));

            sm.DrawSprite(
                texture,
                Position + Offset + centerOffset,
                Tint,
                Rotation,
                realCenter,
                innerScale * Scale,
                Effect,
                Depth,
                Shader,
                Matrix.Identity);

            base.Draw(gameTime);
        }
    }
}
